import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class Student{
    public String classId;
    public String studentId;
}
@WebServlet("/PostStudent")
public class PostStudent extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //使用JSON格式的请求
        resp.setContentType("text/html;charset=utf8");
        ObjectMapper objectMapper = new ObjectMapper();
        // 通过JSON第三方库中的 ObjectMapper类来从请求的 body 中进行读取, 并解析.
        // 使用 readValue 来把 json 字符串转成 Java 对象.
        // 第一个参数是一个 String 或者是一个 InputStream
        // 第二个参数是转换的结果对应的 Java 类对象
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        System.out.println(student.classId + ", " + student.studentId);
        resp.getWriter().write(student.classId + ", " + student.studentId);


    }
}
